package com.modules.service.sys.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.constant.CommonConstant;
import com.common.util.CommonUtil;
import com.common.vo.Result;
import com.modules.dao.sys.OrganizationDao;
import com.modules.dto.sys.resp.TreeResp;
import com.modules.entity.sys.OrganizationEntity;
import com.modules.service.sys.OrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * service组织
 *
 * @author yyq
 */
@Slf4j
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationDao, OrganizationEntity> implements OrganizationService{


    /**
     * 获取树形表格
     * @param entity
     * @return
     */
    public Result<?> selecTreeTable(OrganizationEntity entity) {
        entity.setDelFlag(CommonConstant.ZERO);
        List<OrganizationEntity> list = baseMapper.selectTreeAll(entity);
        if(StrUtil.isNotBlank(entity.getOname())) {
            List<OrganizationEntity> resList = CollUtil.newArrayList();
            for(OrganizationEntity organizationEntity : list) {
                if(organizationEntity.getOname().indexOf(entity.getOname()) != -1) {
                    resList.add(organizationEntity);
                    setOnameList(list, resList, organizationEntity.getOid());
                }
            }
            return Result.page(resList, Long.valueOf(resList.size()));
        }
        return Result.page(list, Long.valueOf(list.size()));
    }

    /**
     * 获取它下面的所有节点
     * @param list
     * @param resList
     * @param oid
     */
    public void setOnameList(List<OrganizationEntity> list, List<OrganizationEntity> resList, Integer oid){
        for(OrganizationEntity organizationEntity : list) {
            if(organizationEntity.getParentId().equals(oid)) {
                resList.add(organizationEntity);
                setOnameList(list, resList, organizationEntity.getOid());
            }
        }
    }


    /**
     * 获取OID
     * @param code
     * @param pass
     * @return
     */
    public String getOid(String code, String pass){
        String oid = CommonUtil.oidMap.get(code);
        if(StrUtil.isBlank(oid)){
            LambdaQueryWrapper<OrganizationEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(OrganizationEntity::getCode, code);
            wrapper.eq(OrganizationEntity::getPass, pass);
            OrganizationEntity entity = baseMapper.selectOne(wrapper);
            if(entity == null){
                oid = entity.getOid().toString();
                CommonUtil.oidMap.put(code, oid);
            }
        }
        return oid;
    }

    /**
     * 获取树形所有
     * @return
     */
    @Override
    public Result<?> selectTreeAll(OrganizationEntity entity) {
        entity.setDelFlag(CommonConstant.ZERO);
        List<OrganizationEntity> list = baseMapper.selectTreeAll(entity);
        ArrayList<TreeResp> newList = CollUtil.newArrayList();
        for(OrganizationEntity organizationEntity : list) {
            if(organizationEntity.getOid().equals(entity.getOid())) {
                List<TreeResp> child = selectTreeChildrendAll(list, organizationEntity.getOid());
                newList.add(new TreeResp(organizationEntity.getOid(), CollUtil.isNotEmpty(child)?child:null, organizationEntity.getOname(), true, organizationEntity.getOname(), null, null, false, true));
            }
        }
        return Result.page(newList, Long.valueOf(newList.size()));
    }

    /**
     * 获取它下面的所有节点
     * @param list
     * @param oid
     */
    public List<TreeResp> selectTreeChildrendAll(List<OrganizationEntity> list, Integer oid){
        List<TreeResp> newList = CollUtil.newArrayList();
        for(OrganizationEntity organizationEntity : list) {
            if(organizationEntity.getParentId().equals(oid)) {
                List<TreeResp> child = selectTreeChildrendAll(list, organizationEntity.getOid());
                newList.add(new TreeResp(organizationEntity.getOid(), CollUtil.isNotEmpty(child)?child:null, organizationEntity.getOname(), true, organizationEntity.getOname(), null, null, false, true));
            }
        }
        return newList;
    }

    /**
     * 更改状态
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> updateState(OrganizationEntity entity) {
        entity.setDelFlag(CommonConstant.ZERO);
        List<OrganizationEntity> list = baseMapper.selectTreeAll(entity);
        List<Integer> collect = list.stream().map(OrganizationEntity::getOid).collect(Collectors.toList());
        OrganizationEntity organizationEntity = new OrganizationEntity();
        organizationEntity.setState(entity.getState());
        LambdaQueryWrapper<OrganizationEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.in(OrganizationEntity::getOid, collect);
        int count = baseMapper.update(entity, lambdaQuery);
        return count > 0 ? Result.success():Result.fail();
    }



}
